<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
        <el-form-item label="排产日期" prop="produceDateRange">
          <el-date-picker
            v-model="page.query.produceDateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="排产编号" prop="produceNo">
          <el-input v-model="page.query.produceNo" placeholder="请输入排产编号" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="page.query.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productId">
          <product-query ref="productQuery" v-model="page.query.productId" @choose="handleChooseProduct" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerId">
          <customer-query ref="customerQuery" v-model="page.query.customerId" @choose="handleChooseCustomer" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <dict-select ref="dictSelect1" v-model="page.query.priority" type="button" dict-key="priority" placeholder="请选择优先级" @input="handleDictSelectChange" />
        </el-form-item>
        <el-form-item label="来源" prop="sourceType">
          <dict-select ref="dictSelect2" v-model="page.query.sourceType" type="button" dict-key="produce_source_type" placeholder="请选择来源" @input="handleDictSelectChange" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-tabs v-model="page.query.produceState" @tab-click="handleTabClick">
        <el-tab-pane
          v-for="(item,index) in produceStateList"
          :key="index"
          :label="item.stateName+'('+item.count+')'"
          :name="item.state"
        />
      </el-tabs>
      <!--      <div>
        <span>排产信息列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button type="primary" class="el-icon-plus" @click="saveClick">新增</el-button>
        <el-button type="warning" class="el-icon-edit" plain @click="updateClick('tableBtn',selected)">修改生产</el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">
          批量删除
        </el-button>
        <el-button type="warning" plain class="el-icon-video-pause" @click="handleBatchPause">
          暂停生产
        </el-button>
        <el-button type="primary" plain class="el-icon-video-play" @click="handleBatchRecovery">
          恢复生产
        </el-button>
        <el-button type="success" plain class="el-icon-printer" @click="openPrintDetail">打印生产单</el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <!--  :span-method="spanMethod"    -->
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" fixed />
        <el-table-column width="50" fixed>
          <template slot-scope="scope">
            <div v-if="scope.row.produceState==='0'">
              <svg-icon v-if="scope.row.priority==='1'" icon-class="jiaji" style="font-size: 20px;width:20px;display: inline;" />
              <svg-icon v-if="scope.row.priority==='0'" icon-class="putong" style="font-size: 17px;width:20px;display: inline;" />
            </div>
            <div v-else-if="scope.row.produceState==='1'">
              <svg-icon icon-class="suspend" style="font-size: 17px;width:20px;display: inline;" />
            </div>
            <div v-else-if="scope.row.produceState==='2'">
              <svg-icon icon-class="finished" style="font-size: 17px;width:20px;display: inline;" />
            </div>
            <div v-else>
              <svg-icon icon-class="cancel" style="font-size: 17px;width:20px;display: inline;" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="排产编号" prop="produceNo" min-width="110" />
        <el-table-column label="排产日期" prop="produceDate" min-width="100" />
        <el-table-column label="订单编号" prop="orderNo" min-width="110" />
        <el-table-column label="客户名称" prop="customerName" min-width="100" />
        <el-table-column label="产品名称" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.productInfo.moldName }}
          </template>
        </el-table-column>
        <el-table-column label="颜色" prop="color" min-width="60">
          <template slot-scope="scope">
            {{ scope.row.productInfo.color }}
          </template>
        </el-table-column>
        <el-table-column label="材质" prop="material" min-width="60">
          <template slot-scope="scope">
            {{ scope.row.productInfo.material }}
          </template>
        </el-table-column>
        <el-table-column label="尺码" prop="size" min-width="60">
          <template slot-scope="scope">
            {{ scope.row.productInfo.size }}
          </template>
        </el-table-column>
        <el-table-column label="长度" prop="length" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.productInfo.length }}
          </template>
        </el-table-column>
        <el-table-column label="总数量" prop="quantity" min-width="80" />
        <el-table-column label="剩余数量" prop="remainingQuantity" min-width="80" />
        <el-table-column label="生产进度" min-width="150">
          <template slot-scope="scope">
            <el-progress :percentage="scope.row.percentage" :stroke-width="8" />
          </template>
        </el-table-column>
        <el-table-column label="生产状态" prop="produceState" min-width="100">
          <template slot-scope="scope">
            <produce-state-tag v-model="scope.row.produceState" />
          </template>
        </el-table-column>
        <el-table-column label="来源" prop="sourceType" min-width="100">
          <template slot-scope="scope">
            <produce-source-type-tag v-model="scope.row.sourceType" />
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip min-width="100" />
        <el-table-column label="操作" min-width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              :disabled="scope.row.produceState==='2'"
              @click="updateClick('column',scope.row)"
            >修改</el-button>
            <el-button
              :disabled="scope.row.produceState==='2'"
              type="text"
              @click="outputClick(scope.row)"
            >产量登记
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
    <div v-if="outputVisible">
      <submit-output-module ref="submitOutputModule" :visible="outputVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { selectProducePage, deleteProduce, pauseProduce, recoveryProduce, selectProduceStateCount } from '@/api/erp/produce'
import submitModule from '@/views/erp/produce/modules/submitModule.vue'
import SubmitOutputModule from '@/views/erp/produce/modules/submitOutputModule.vue'

export default {
  name: 'Index',
  components: { SubmitOutputModule, submitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      produceStateList: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          productId: undefined,
          produceNo: '',
          orderNo: '',
          produceDateRange: [],
          customerId: undefined,
          priority: '',
          produceState: '0',
          sourceType: ''
        }
      },
      dialogVisible: false,
      outputVisible: false
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectProducePage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
      this.selectProduceStateCount()
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.customerId = undefined
      this.page.query.productId = undefined
      this.page.query.produceState = '-1'
      this.$refs.customerQuery.resetQueryForm()
      this.$refs.productQuery.resetQueryForm()
      this.$refs.dictSelect1.resetActive()
      this.$refs.dictSelect2.resetActive()
      this.doPage()
    },
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    // 修改按钮点击
    updateClick(type, row) {
      let id
      if (type === 'tableBtn') {
        if (this.selected.length === 0) {
          this.$message.warning('请选择一条数据')
          return
        } else if (this.selected.length !== 1) {
          this.$message.warning('只能选择一条数据')
          return
        }
        id = this.selected[0].id
      } else {
        id = row.id
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(id)
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.dialogVisible = false
      this.outputVisible = false
      this.doPage()
    },
    tableRowClassName({ row }) {
      /* if (row.priority === '1') {
        return 'danger-row'
      } else if (row.priority === '0') {
        return ''
      } */
      return ''
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 4) {
      if (column.label === '客户名称') {
        // 获取当前单元格的值
        const currentValue = row[column.property]
        // 获取上一行相同列的值
        const preRow = this.tableData[rowIndex - 1]
        const preValue = preRow ? preRow[column.property] : null
        // 如果当前值和上一行的值相同，则将当前单元格隐藏
        if (currentValue === preValue) {
          return { rowspan: 0, colspan: 0 }
        } else {
          // 否则计算当前单元格应该跨越多少行
          let rowspan = 1
          for (let i = rowIndex + 1; i < this.tableData.length; i++) {
            const nextRow = this.tableData[i]
            const nextValue = nextRow[column.property]
            if (nextValue === currentValue) {
              rowspan++
            } else {
              break
            }
          }
          return { rowspan, colspan: 1 }
        }
      }
    },
    handleChooseCustomer(row) {
      if (row) {
        this.page.query.customerId = row.id
      }
    },
    handleChooseProduct(row) {
      if (row) {
        this.page.query.productId = row.id
      }
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据,是否继续?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deleteProduce(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    // 批量暂停点击
    handleBatchPause() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量暂停选中的数据,是否继续?', '暂停提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        pauseProduce(data).then(resp => {
          if (resp.success) {
            this.$message.success('暂停成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    // 批量恢复生产
    handleBatchRecovery() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量恢复选中的数据,是否继续?', '恢复生产提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        recoveryProduce(data).then(resp => {
          if (resp.success) {
            this.$message.success('恢复生产成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    selectProduceStateCount() {
      this.tableLoading = true
      selectProduceStateCount(this.page.query).then(resp => {
        if (resp.success) {
          this.produceStateList = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleTabClick(tab) {
      this.page.query.produceState = tab.name
      this.doPage()
    },
    handleDictSelectChange() {
      this.doPage()
    },
    // 产量
    outputClick(row) {
      this.outputVisible = true
      this.$nextTick(() => {
        this.$refs.submitOutputModule.init(row)
      })
    },
    openPrintDetail() {
      const routeData = this.$router.resolve({
        path: '/erp/produce/print',
        query: {
          // ...this.page.query,
          'token': sessionStorage.getItem('authorization')
        }
      })
      window.open(routeData.href, '_blank')
    }
  }
}
</script>

<style scoped>

</style>
